#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <vector>
#include<algorithm>
using namespace std;
char* intToStr(int x)
{
    char* result = new char[10];
    string str = to_string(x);
    int i = 0;
    while(i < str.length())
    {
        result[i] = str[i];
        i++;
    }
    result[i] = '\0';
    return result;
}
vector<char*> sort(vector<char*> in , char* rem)
{
    char c[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    vector <int> comp1;
    vector <int> comp2; 
    int len = 0;
    for(int i = 0 ; rem[i] ; i++)
        len ++;
    for(int i = 0 ; i < in.size() ; i++)
    {
        int temp = 0;
        for(int j = len ; in[i][j] != '.' ; j++)
        {
            for(int k = 0 ; k < 10 ; k++)
            {
                if(in[i][j] == c[k])
                {
                    temp = temp * 10 + k;
                    break;
                }
            }
        }
        comp1.push_back(i);
        comp2.push_back(temp);
    }
    for(int i = 0 ; i < in.size() ; i++)
    {
        for(int j = i + 1 ; j < in.size() ; j++)
        {
            if(comp2[j] < comp2[i])
            {
                int temp1 = comp1[i];
                int temp2 = comp2[i];
                comp1[i] = comp1[j];
                comp1[j] = temp1;
                comp2[i] = comp2[j];
                comp2[j] = temp2;
            }
        }
    }
    vector<char*> result;
    for(int i = 0 ; i < comp1.size() ; i++)
        result.push_back(in[comp1[i]]);
    return result;
}
int filesNum(char* path)
{
    DIR *d;
    struct dirent *dir;
    d = opendir(path);
    int counter = 0;
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            if(strcmp(dir -> d_name , ".") != 0  && strcmp(dir -> d_name , "..") != 0)
            {
                counter ++;
            }
        }
        closedir(d);
    }
    return counter;
}
int main(int argc, char** argv) 
{
    int size = filesNum(argv[1]);
    cout<<size<<endl;
    DIR *d;
    struct dirent *dir;
    d = opendir(argv[1]);
    int rf;
    int counter = 1;
    vector<char*> ipaths;
    vector<char*> fpaths;
    char* ifiles[size];
    char* ffiles[size];
    char sortC[64];
    strcpy(sortC,argv[1]);
    for(int i = 0 ; i < size ; i++)
    {
        ifiles[i] = new char[64];
        ipaths.push_back(ifiles[i]);
        ffiles[i] = new char[64];
        fpaths.push_back(ffiles[i]);
    }
    if (d)
    {
        while ((dir = readdir(d)) != NULL)
        {
            
            char name[64];
            char ipath[64];
            char fpath[64];
            strcpy(ipath,argv[1]);
            strcpy(fpath,argv[2]);
            strcpy(name,argv[3]);
            strcat(name , intToStr(counter));
            strcat(name , argv[4]);
            if(strcmp(dir -> d_name , ".") != 0  && strcmp(dir -> d_name , "..") != 0)
            {
                strcat(ipath , dir->d_name);
                strcat(fpath, name);
                strcpy(ipaths[counter-1],ipath);
                strcpy(fpaths[counter-1],fpath);
                counter++;
            }
            /*
                ifstream file1(ipath);
                ofstream file2(fpath);
                string temp;
                while (getline (file1, temp))
                    file2<<temp<<endl;
                file1.close();
                file2.close();
                cout<<ipath<<endl;
                cout<<fpath<<endl;*/
    }
    strcat(sortC,argv[5]);
    ipaths = sort(ipaths , sortC);
    for(int i = 0 ; i < ipaths.size(); i++)
    {
        ifstream file1(ipaths[i]);
        ofstream file2(fpaths[i]);
        string temp;
        while (getline (file1, temp))
            file2<<temp<<endl;
        file1.close();
        file2.close();
        /*rf = rename(ipaths[i] , fpaths[i]);
        if(rf != 0)
            cout<<"Unseccessful renamaing operation!!\n";*/
    }
    closedir(d);
    }
    for(int i = 0 ; i < ipaths.size() ; i++)
        cout<<ipaths[i]<<endl;
return(0);
}
